package ysq.xyg.web.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.web.dao.WebScoreRecordDao;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.model.po.WebScoreRecord;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebScoreRecordVo;
import ysq.xyg.web.service.WebScoreRecordService;

import java.util.Date;
import java.util.List;

/**
 * 业务模块-用户账单记录service实现
 *
 * @author congcong tao
 * @date 2020-1-11 19:03:23
 */
@Service("webScoreRecordService")
public class WebScoreRecordServiceImpl extends BaseServiceImpl<WebScoreRecord> implements WebScoreRecordService {

    @Autowired
    private WebScoreRecordDao webScoreRecordDao;

    @Autowired
    public void setBaseDao(WebScoreRecordDao webScoreRecordDao) {
        this.baseDao = webScoreRecordDao;
    }

    /**
     * 分页查询
     *
     * @param webScoreRecordVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2020-1-11 19:03:23
     */
    @Override
    public ReturnEntity page(WebScoreRecordVo webScoreRecordVo, int pageSize, int pageNum) {
        if (StrUtil.isNotEmpty(webScoreRecordVo.getMemberNickName())) {
            webScoreRecordVo.setMemberNickName(BaseUtil.encodeBase64String(webScoreRecordVo.getMemberNickName()));
        }
        PageHelper.startPage(pageNum,pageSize);
        List<WebScoreRecord> list = webScoreRecordDao.selectEntityList(webScoreRecordVo);
        if (CollUtil.isNotEmpty(list)) {
            for (WebScoreRecord webScoreRecord : list) {
                if (StrUtil.isNotEmpty(webScoreRecord.getMemberNickName())) {
                    webScoreRecord.setMemberNickName(BaseUtil.decodeBase64(webScoreRecord.getMemberNickName()));
                }
            }
        }
        PageInfo<WebScoreRecord> page = new PageInfo<WebScoreRecord>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webScoreRecordVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2020-1-11 19:03:23
     */
    @Override
    public ReturnEntity addOrUpdate(WebScoreRecordVo webScoreRecordVo, SysUser currentUser) {
        boolean isUpate = true;
        WebScoreRecord webScoreRecord;
        if (StrUtil.isEmpty(webScoreRecordVo.getId())) {
            webScoreRecord = new WebScoreRecord(currentUser);
            isUpate = false;
        } else {
            webScoreRecord = selectByPrimaryKey(webScoreRecordVo.getId());
            if (ObjectUtil.isNull(webScoreRecord)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            webScoreRecord.setUpdateTime(new Date());
            webScoreRecord.setUpdateUser(currentUser.getId());
        }
        webScoreRecord.setMemberId(webScoreRecordVo.getMemberId());
        webScoreRecord.setScore(webScoreRecordVo.getScore());
        webScoreRecord.setRecordType(webScoreRecordVo.getRecordType());
        webScoreRecord.setDescripts(webScoreRecordVo.getDescripts());
        webScoreRecord.setExtraId(webScoreRecordVo.getExtraId());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(webScoreRecord);
        } else {
            num = insert(webScoreRecord);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2020-1-11 19:03:23
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebScoreRecord webScoreRecord = selectByPrimaryKey(id);
        if (webScoreRecord != null) {
            webScoreRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webScoreRecord.setUpdateTime(new Date());
            webScoreRecord.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(webScoreRecord);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webScoreRecordVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2020-1-11 19:03:23
     */
    @Override
    public List<WebScoreRecord> acceptList(WebScoreRecordVo webScoreRecordVo){
        Example example = new Example(WebScoreRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(webScoreRecordVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", webScoreRecordVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webScoreRecordDao.selectByExample(example);
    }

    @Override
    public Double selectAllCount(WebScoreRecord recoveryRecordEntrys) {
        return webScoreRecordDao.selectAllCount(recoveryRecordEntrys);
    }
}
